<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 发送短信
 * @param type $telphone 
 * @param type $params 
 * @param type $sign_name
 * @param type $template_code
 * @return boolean
 */
function sendSMS($telphone,$params='测试',$sign_name='大鱼测试',$template_code = 'SMS_7990037') {
        $config = C('ALIDAYU_SETTING');
        vendor('Alidayu.Autoloader');
        $c = new \TopClient;
        $c->format = 'json';
        $c->appkey = $config['ak'];
        $c->secretKey = $config['sk'];
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setSmsType("normal");
        $req->setSmsFreeSignName($sign_name);
        $req->setSmsParam(json_encode($params));
        $req->setRecNum($telphone);
        $req->setSmsTemplateCode($template_code);
        $resp = $c->execute($req);
        if(isset($resp->result->success) && $resp->result->success){
        return true;
        }else{
            return false;
        }
}

/**
 * 将错误信数组转换为字符串
 * @param $errors
 * @return string
 */
function get_error($errors){
    //判断是否是数组
    if(is_array($errors)){
        $html = '<ol>';
        foreach($errors as $error){
            $html.='<li>'.$error.'</li>';
        };
        $html.= '</ol>';
        return $html;
    } else {
        return $errors;
    }
}

/**
 * md5加密
 * @param type $password 密码
 * @param type $salt 盐
 * @return type md5加密后的密码
 */

function salt_password($password,$salt){
    return md5($password.$salt);
}

function sendMail($to, $title, $content) {
     
        Vendor('PHPMailer.PHPMailerAutoload');   
        $config = C('EMAIL');
        $mail = new \PHPMailer(); //实例化
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host=$config['MAIL_HOST']; //smtp服务器的名称（这里以QQ邮箱为例）
        $mail->SMTPAuth = $config['MAIL_SMTPAUTH']; //启用smtp认证
        $mail->Username = $config['MAIL_USERNAME']; //你的邮箱名
        $mail->Password = $config['MAIL_PASSWORD'] ; //邮箱密码
        $mail->From = $config['MAIL_FROM']; //发件人地址（也就是你的邮箱地址）
        $mail->FromName = $config['MAIL_FROMNAME']; //发件人姓名
        $mail->AddAddress($to,"尊敬的客户");
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML($config['MAIL_ISHTML']); // 是否HTML格式邮件
        $mail->CharSet=$config['MAIL_CHARSET']; //设置邮件编码
        $mail->Subject =$title; //邮件主题
        $mail->Body = $content; //邮件内容
        $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
//        return($mail->Send());
       dump($mail->send());
    }